<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="dbpool.DbcpConnectionPool" %>
<%--
  Created by IntelliJ IDEA.
  User: 86147
  Date: 2024/4/14
  Time: 17:30
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>处理登录操作</title>
</head>
<body>
<%
    request.setCharacterEncoding("utf-8");
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    // 1加载JDBC驱动
    try{
        // 2建立连接
        conn = DbcpConnectionPool.getConnection();
        // 3创建对象
        String sql = "select password from user where username='"+username+"'";
        stmt = conn.createStatement();
        // 4执行，分析结果
        rs = stmt.executeQuery(sql);
        if(rs.next()) { // 判断数据库是否存在此用户
            if(password.equals(rs.getString(1))){
                session.setAttribute("username",username);
                response.sendRedirect(request.getContextPath() + "/WebContent/client/loginsuccess.jsp");
            } else {
                session.setAttribute("Message", "账号密码错误");
                response.sendRedirect(request.getContextPath() + "/WebContent/client/loginfail.jsp");
            }
        }else {
            session.setAttribute("Message", "账号密码错误");
            response.sendRedirect(request.getContextPath() + "/WebContent/client/loginfail.jsp");
        }

    }catch (Exception e){
        e.printStackTrace();
    }finally {
        // 5归还连接
        DbcpConnectionPool.close(rs,stmt,conn);
    }
%>
</body>
</html>
